//
//  ComicsTypeListCell.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/12.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class ComicsTypeListCell: UITableViewCell {

    var spinnerName: String?
    
    fileprivate lazy var iconView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.clipsToBounds = true
        return imgV
    }()
    
    fileprivate lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        return label
    }()
    
    fileprivate lazy var subTitleLabel: UILabel = {
       let label = UILabel()
        label.textColor = UIColor.gray
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var descLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.numberOfLines = 3
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var tagLabel: UILabel = {
       let label = UILabel()
        label.textColor = UIColor.orange
        label.font = UIFont .systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var orderView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        return imgV
    }()
    
    fileprivate func setUpSnap() {
        separatorInset = .zero
        self.iconView.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview().inset(UIEdgeInsetsMake(10, 10, 10, 0))
            make.width.equalTo(100)
        }
        
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(self.iconView)
            make.height.equalTo(30)
        }
        
        self.subTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(20)
            make.top.equalTo(self.titleLabel.snp.bottom).offset(5)
        }
        
        self.descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(60)
            make.top.equalTo(self.subTitleLabel.snp.bottom).offset(5)
        }
        
        self.orderView.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconView.snp.bottom)
            make.height.width.equalTo(30)
            make.right.equalToSuperview().offset(-10)
        }
        
        self.tagLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconView.snp.right).offset(10)
            make.right.equalTo(orderView.snp.left).offset(-10)
            make.height.equalTo(20)
            make.bottom.equalTo(iconView.snp.bottom)
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(self.iconView)
        self.contentView.addSubview(self.titleLabel)
        self.contentView.addSubview(self.subTitleLabel)
        self.contentView.addSubview(self.descLabel)
        self.contentView.addSubview(self.orderView)
        self.contentView.addSubview(self.tagLabel)
        setUpSnap()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var model: ComicsTypeModel? {
        didSet{
            guard let model = model else {
                return
            }
            self.iconView.kf.setImage(urlString: model.cover)
            self.titleLabel.text = model.name
            self.subTitleLabel.text = "\(model.tags?.joined(separator: " ") ?? "") | \(model.author ?? "")"
            self.descLabel.text = model.description
            
            if spinnerName == "更新时间" {
                //日期 int 转成 TimeInterval
                let comicDate = Date().timeIntervalSince(Date(timeIntervalSince1970: TimeInterval(model.conTag)))
                var tagString = ""
                if comicDate < 60 {
                    tagString = "\(Int(comicDate))秒前"
                }else if comicDate < 3600 {
                    tagString = "\(Int(comicDate / 60))分前"
                } else if comicDate < 86400 {
                    tagString = "\(Int(comicDate / 3600))小时前"
                } else if comicDate < 31536000{
                    tagString = "\(Int(comicDate / 86400))天前"
                } else {
                    tagString = "\(Int(comicDate / 31536000))年前"
                }
                tagLabel.text = "\(spinnerName!) \(tagString)"
                orderView.isHidden = true
            }else {
                var tagString = ""
                if model.conTag > 100000000 {
                    tagString = String(format: "%.1f亿", Double(model.conTag) / 100000000)
                }else if model.conTag > 10000 {
                    tagString = String(format: "%.1f万", Double(model.conTag) / 10000)
                } else {
                    tagString = "\(model.conTag)"
                }
                if tagString != "0" { tagLabel.text = "\(spinnerName ?? "总点击") \(tagString)" }
                orderView.isHidden = false
            }
            
        }
    }
    
    
    
    var indexPath: IndexPath? {
        didSet{
            guard indexPath != nil else {
                return
            }
            if indexPath?.row == 0 { orderView.image = UIImage.init(named: "rank_frist") }
            else if indexPath?.row == 1 { orderView.image = UIImage.init(named: "rank_second") }
            else if indexPath?.row == 2 { orderView.image = UIImage.init(named: "rank_third") }
            else { orderView.image = nil }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
